# -*- coding: utf-8 -*-
import json
import time, datetime

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from ChineseLottery.items import LotteryNews


class LotteryNewsSpider(CrawlSpider):
    name = 'lottery_news'
    allowed_domains = ['dongqiudi.com']

    def start_requests(self):
        for page in range(1, 11):
            page_url = 'http://api.dszuqiu.com/v9/dongtai/discovery?type_id=10&set_as_read=1&page=' + str(page) + '&token=&per_page=10'
            yield scrapy.Request(url=page_url, callback=self.parse_page)

    # 获取DS足球热门资讯
    def parse_page(self, response):
        json_object = json.loads(response.body.decode('utf-8'))
        all = json_object.get('dongtai')
        for article in all:
            news = LotteryNews()
            news['title'] = article.get('title')
            news['author'] = article.get('user').get('username')
            time_array = time.localtime(int(article.get('update_time'))-28800)
            news['time'] = time.strftime('%Y-%m-%d %H:%M', time_array)
            news['content'] = article.get('dongtai')
            yield news
